package com.tsinghuabigdata.edu.ddmath.requestHandler.requestImpl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tsinghuabigdata.edu.commons.http.HttpRequest;
import com.tsinghuabigdata.edu.commons.http.HttpRequestException;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppRequest;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppRequestUtils;
import com.tsinghuabigdata.edu.ddmath.constant.AppRequestConst;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.ApplyTutor;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.OnlineTutoring;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.UploadImage;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorApplied;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorQueue;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorState;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorTurn;
import com.tsinghuabigdata.edu.ddmath.requestHandler.TutorService;

import org.json.JSONException;

import java.util.List;

/**
 * Created by Administrator on 2017/3/11.
 */

public class TutorServiceImpl extends BaseService implements TutorService {

    @Override
    public List<OnlineTutoring> queryTeacherState(String access_token, String classId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.QUERY_TEACHER_STATE);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
                .putRequestParam("classId", classId);
        String res = request.request().getBody();
        return new Gson().fromJson(res, new TypeToken<List<OnlineTutoring>>() {
        }.getType());
    }

    @Override
    public TutorQueue queryTutorQueue(String access_token, String studentId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.QUERY_TUTOR_QUEUE);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
                .putRequestParam("studentId", studentId);
        String res = request.request().getBody();
        return new Gson().fromJson(res, TutorQueue.class);
    }

    @Override
    public ApplyTutor applyTutor(String access_token, String studentId, String teacherId, String imageIds, int estimateTime) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.APPLY_TUTOR);
        HttpRequest request = AppRequestUtils.post(url);
        request.putHeader("access_token", access_token)
                .putJsonParam("studentId", studentId)
                .putJsonParam("teacherId", teacherId)
                .putJsonParam("imageIds", imageIds)
                .putJsonParam("estimateTime", estimateTime + "");
        String res = request.requestJson().getBody();
        return new Gson().fromJson(res, ApplyTutor.class);
    }

    @Override
    public TutorApplied isTutorApplied(String access_token, String studentId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.IS_TUTOR_APPLIED);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
                .putRequestParam("studentId", studentId);
        String res = request.request().getBody();
        return new Gson().fromJson(res, TutorApplied.class);
    }

    @Override
    public TutorTurn isTutorTurn(String access_token, String studentId, String teacherId) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.IS_TUTOR_TURN);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
                .putRequestParam("studentId", studentId)
                .putRequestParam("teacherId", teacherId);
        String res = request.request().getBody();
        return new Gson().fromJson(res, TutorTurn.class);
    }

    @Override
    public TutorState modifyTutorState(String access_token, String coachId, int state, String remark) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.MODIFY_TUTOR_STATE);
        HttpRequest request = AppRequestUtils.put(url);
        request.putHeader("access_token", access_token)
                .putRestfulParam("coachId", coachId)
                .putRequestParam("state", state + "")
                .putRequestParam("remark", remark);
        String res = request.request().getBody();
        return new Gson().fromJson(res, TutorState.class);
    }

    @Override
    public List<UploadImage> queryUploadImages(String access_token, int pageNum, int pageSize, String studentId) throws HttpRequestException, JSONException {
        String url = getUpgradeApkUrl(AppRequestConst.QUERY_UPLOAD_IMAGES);
        HttpRequest request = AppRequestUtils.get(url);
        request.putHeader("access_token", access_token)
                .putRequestParam("pageNum", pageNum + "")
                .putRequestParam("pageSize", pageSize + "")
                .putRequestParam("studentId", studentId);
        String res = request.request().getBody();
        return new Gson().fromJson(res, new TypeToken<List<UploadImage>>() {
        }.getType());
    }

    @Override
    public TutorState startTutor(String access_token, String coachId, String remark) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.START_TUTOR)+"&remark="+remark;
        HttpRequest request = AppRequestUtils.put(url);
        request.putHeader("access_token", access_token)
                .putRestfulParam("coachId", coachId);
        //                .putFrom("remark", remark);
        ((AppRequest) request).getHeader().put("Content-Type", "application/json; charset=UTF-8");
        String res = request.requestJson().getBody();
        return new Gson().fromJson(res, TutorState.class);
    }

    @Override
    public TutorState endTutorNormal(String access_token, String coachId, String remark) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.END_TUTOR_NORMAL);
        HttpRequest request = AppRequestUtils.put(url);
        request.putHeader("access_token", access_token)
                .putRestfulParam("coachId", coachId)
                .putRequestParam("remark", remark);
        String res = request.requestJson().getBody();
        return new Gson().fromJson(res, TutorState.class);
    }

    @Override
    public TutorState endTutorException(String access_token, String coachId, String remark) throws HttpRequestException, JSONException {
        String url = getUrl(AppRequestConst.END_TUTOR_EXCEPTION);
        HttpRequest request = AppRequestUtils.put(url);
        request.putHeader("access_token", access_token)
                .putRestfulParam("coachId", coachId)
                .putRequestParam("remark", remark);
        String res = request.requestJson().getBody();
        return new Gson().fromJson(res, TutorState.class);
    }
}
